package com.fh.controller.loantask;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.fh.controller.base.BaseController;
import com.fh.model.Page;
import com.fh.service.blacklist.BlackListServiceImpl;
import com.fh.service.loantaskimpl.LoanTaskToBeServiceImpl;
import com.fh.service.system.user.UserService;
import com.fh.util.Const;
import com.fh.util.JsonUtil;
import com.fh.util.PageData;
import com.fh.util.R;
import com.fh.util.UserUtils;

import net.sf.json.JSONArray;


/**
 * 黑名单审批
 * @author dell、
 *
 */

@Controller
@RequestMapping(value="/blackName")
public class BlackNameCheckController extends BaseController{

	@Autowired
	private BlackListServiceImpl blackListServiceImpl;
	
	@Autowired
    private UserService userService;
	
	@Autowired
	private LoanTaskToBeServiceImpl loanTaskToBeService;
	
	@RequestMapping(value="/blackNameList")
	public ModelAndView blackNameCheckList(Page page, HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		
		PageData pd = loanTaskToBeService.setPdWithTrim(this.getPageData());
		
		Map<String, String> map1 = UserUtils.getCurrentRole();
        Integer QX1 = Integer.valueOf(map1.get(Const.SESSION_QX1));

        page.setPd(UserUtils.setDataRole(pd, userService));
		page.setPd(pd);
		
		R.setPageHelper(pd);
		List<PageData> pageList = blackListServiceImpl.selectBlackNamelistPage(page);
		R.setPage(page, pageList);
		mv.addObject("pageList", pageList);
        mv.addObject("pd", pd);
        mv.addObject(Const.SESSION_QX1, QX1);
		mv.setViewName("loantask/blackNameList");
		return mv;
	}
	/**
	 * 审核通过和拒绝
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/checkStatus")
	public ModelAndView checkStatus(Page page,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		
		List<Map<String,String>> resultList=new ArrayList<Map<String,String>>();
		Map<String,Object> map=new HashMap<String,Object>();
		
		PageData pd = new PageData();
        pd = this.getPageData();
        page.setPd(pd);
        page.setPd(UserUtils.setDataRole(pd,userService));
		
		String id=request.getParameter("id");
		String checkStatus=request.getParameter("status");
		String contIdCard=request.getParameter("contIdCardB");
		String productSource=request.getParameter("productSourceB");
		
		if(!StringUtils.isEmpty(id)){
			if("1".equals(checkStatus)){//审核通过
				//此处写接口
				String result=null;
				if(org.apache.commons.lang.StringUtils.isNotBlank(contIdCard)){
					Map<String, String> mapp=new HashMap<String,String>();
					mapp.put("idCardNumber", contIdCard);
					mapp.put("productCode", productSource);
					resultList.add(mapp);
					result=blackListServiceImpl.riskBlankName(resultList);//黑名单调用风控接口
					if("200".equals(JsonUtil.jsonToMap(result).get("code"))){
						map.put("isNoStatus", "0");//不展示
						map.put("status", "1");//通过
						map.put("id", id);
						blackListServiceImpl.updateStatus(map);
						mv.setViewName("loantask/blackNameList");
					}else{
						logger.info("审核失败==>"+result);
						mv.setViewName("loantask/doError");
						pd.put("msg", "黑名单审核失败！失败原因：1接口网络异常；2该用户被重复提交黑名单");
					}
				}else{
					mv.setViewName("loantask/doError");
					pd.put("msg", "身份证号为空，不能通过风控接口审核！");
				}
			}else if("2".equals(checkStatus)){//审核拒绝
				map.put("isNoStatus", "0");
				map.put("status", "2");//审核拒绝
				map.put("id", id);
				blackListServiceImpl.updateStatus(map);
				mv.setViewName("loantask/blackNameList");
			}
		}else{
			mv.setViewName("loantask/doError");
			pd.put("msg", "请返回选择需要审核的黑名单信息！");
		}
		
		List<PageData> pageList = blackListServiceImpl.selectBlackNamelistPage(page);
		mv.addObject("pageList", pageList);
        mv.addObject("pd", pd);
		return mv;
	}
	/**
	 * 批量审核通过和拒绝
	 * @param page
	 * @param request
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/checkStatusList")
	public ModelAndView checkStatusList(Page page, HttpServletRequest request,@RequestParam("ids")String[] ids,
			@RequestParam("contIdCards")String[] contIdCards,@RequestParam("productSourceB")String[] productSourceB){
		ModelAndView mv=new ModelAndView();
		Map<String,Object> map=new HashMap<String,Object>();
		
		PageData pd = new PageData();
        pd = this.getPageData();
        page.setPd(pd);
        page.setPd(UserUtils.setDataRole(pd,userService));
		
		String checkStatus=request.getParameter("status");
		
		List<Map<String,String>> result=new ArrayList<Map<String,String>>();
		List<String> listTo=Arrays.asList(contIdCards);//数组转List
		listTo.stream().forEach(x->{
			Map<String, String> mapp=new HashMap<String,String>();
			if(org.apache.commons.lang.StringUtils.isNotEmpty(x)){//身份证去空
				mapp.put("idCardNumber", x);
			
				String productCode = productSourceB[listTo.indexOf(x)];
				mapp.put("productCode", productCode);
				result.add(mapp);
			}
		});
		
		if(ids.length != 0){
			if("1".equals(checkStatus)){//审核通过
                //此处写接口
				if(contIdCards.length != 0){
					String result1=blackListServiceImpl.riskBlankName(result);//黑名单调用风控接口
					if("200".equals(JsonUtil.jsonToMap(result1).get("code"))){
						map.put("isNoStatus", "0");//不展示
						map.put("status", "1");//通过
						map.put("ids", ids);
						blackListServiceImpl.updateStatusList(map);
						mv.setViewName("loantask/blackNameList");
					}else{
						logger.info("批量审核失败==>"+result1);
						mv.setViewName("loantask/doError");
						pd.put("msg", "黑名单审核失败！失败原因：1接口网络异常；2该用户被重复提交黑名单");
					}
				}else{
					mv.setViewName("loantask/doError");
					pd.put("msg", "身份证号为空，不能通过风控接口审核！");
				}
			}else if("2".equals(checkStatus)){//审核拒绝
				map.put("isNoStatus", "0");
				map.put("status", "2");//审核拒绝
				map.put("ids", ids);
				blackListServiceImpl.updateStatusList(map);
				mv.setViewName("loantask/blackNameList");
			}
		}else{
			mv.setViewName("loantask/doError");
			pd.put("msg", "请返回选择需要审核的黑名单信息！");
		}
		
		List<PageData> pageList = blackListServiceImpl.selectBlackNamelistPage(page);
		mv.addObject("pageList", pageList);
        mv.addObject("pd", pd);
		return mv;
	}
}
